<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>注册</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h2>注册</h2>
<form id="regForm">
  用户名：<input type="text" id="username"><br>
  密码：<input type="password" id="password"><br>
  <button type="submit">注册</button>
</form>
<p id="msg"></p>

<script>
  $('#regForm').submit(function (e) {
    e.preventDefault();
    $.ajax({
      url: '/api/register',
      method: 'POST',
      contentType: 'application/json',
      data: JSON.stringify({
        username: $('#username').val(),
        password: $('#password').val()
      }),
      success: function (data) {
        $('#msg').text(data);
      },
      error: function (xhr) {
        $('#msg').text(xhr.responseText);
      }
    });
  });
</script>
</body>
</html>
